package 华为OD机试真题2023.进阶题;

import utils.MyUtil;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.*;

public class HJ19_优雅数组 {
    public static void main(String[] args) throws FileNotFoundException {
        Scanner in = new Scanner(new File("src/main/java/华为OD机试真题2023/进阶题/input/19.txt"));
        MyUtil.START();
        Solution(in);
        MyUtil.END();
    }

    static void Solution(Scanner in) {
        int N = in.nextInt();
        int K = in.nextInt();
        in.nextLine(); // 吸收回车
        String[] sArray = in.nextLine().split(" ");
        int[] nums = new int[N];
        for (int i = 0; i < N; i++) {
            nums[i] = Integer.parseInt(sArray[i]);
        }

        int result = 0, right = 0;
        int[] cnt = new int[N + 1];
        for (int i = 0; i < N; i++) {
            if (i > 0) {
                cnt[nums[i - 1]]--;
            } else {
                cnt[nums[i]]++;
            }
            if (Arrays.stream(cnt).max().getAsInt() >= K) {
                result += N - right;
                continue;
            }
            for (int j = right + 1; j < N; j++) {
                cnt[nums[j]]++;
                if (Arrays.stream(cnt).max().getAsInt() >= K) {
                    right = j;
                    result += N - j;
                    break;
                }
            }
        }
        System.out.println(result);
    }
}
